package com.specialtreasure.freightdepot.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.core.config.MyApp;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.model.GoodsList;
import com.specialtreasure.freightdepot.popupwindow.SelectUnitPW;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/3.
 * 随车货物清单列表
 */

public class GoodsListAdapter extends BaseAdapter {

    private List<GoodsList> goodsLists = new ArrayList<>();
    private String article;//品名及规格

    private Integer quantity;//数量

    private String unit;//单位

    private Context context;

    public GoodsListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (goodsLists != null && goodsLists.size() > 0) {
            return goodsLists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return getItem(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (null == view) {
            view = View.inflate(viewGroup.getContext(), R.layout.item_goods_list, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

       final GoodsList goods = goodsLists.get(i);
       final ViewHolder viewHolderFinal = viewHolder;


        //设置点击事件
        if (i == 0) {
            viewHolder.iv_add.setImageDrawable(MyApp.getInstance().getResources().getDrawable(R.mipmap.btn_increase));
            viewHolder.iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goodsLists.add(new GoodsList());
                    notifyDataSetChanged();
                }
            });

        } else {
            viewHolder.iv_add.setImageDrawable(MyApp.getInstance().getResources().getDrawable(R.mipmap.btn_reduce));
            viewHolder.iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goodsLists.remove(i);
                    notifyDataSetChanged();
                }
            });
        }
        //选择单位
        viewHolder.rl_unit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectUnitPW supw4 = new SelectUnitPW(context, "huowushuliang.xml", "单位");
                supw4.showAtBottom(viewHolderFinal.rl_root);
                supw4.setOnResultListener(new SelectUnitPW.OnResultListener() {
                    @Override
                    public void onResult(String str) {
                        if (!TextUtils.isEmpty(str)) {
                            //单位
                            unit = str;
                            goods.setUnit(unit);
                            viewHolderFinal.tv_unit.setText(unit);
                            viewHolderFinal.tv_unit.setTextColor(context.getResources().getColor(R.color.common_474747));
//                            Logger.e("unit:"+unit);

                        }
                    }
                });

            }
        });

        //品名及规格
        viewHolder.et_goods_cost.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                goods.setArticle(editable.toString());
//                Logger.e("article:"+article);
            }
        });

        //数量
        viewHolder.et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())){
                    quantity=Integer.parseInt(editable.toString());
                    goods.setQuantity(quantity);
//                    Logger.e("quantity:"+quantity);
                }

            }
        });





        return view;
    }


    public List<GoodsList> getGoodsList() {
        return goodsLists;
    }

    public void setGoodsList(List<GoodsList> goodsLists) {
        this.goodsLists = goodsLists;
    }


    public static class ViewHolder {
        public View rootView;
        public EditText et_goods_cost;
        public EditText et_num;
        public TextView tv_unit;
        public RelativeLayout rl_unit;
        public LinearLayout ll_goods;
        public ImageView iv_add;
        public RelativeLayout rl_root;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.et_goods_cost = (EditText) rootView.findViewById(R.id.et_goods_cost);
            this.et_num = (EditText) rootView.findViewById(R.id.et_num);
            this.tv_unit = (TextView) rootView.findViewById(R.id.tv_unit);
            this.rl_unit = (RelativeLayout) rootView.findViewById(R.id.rl_unit);
            this.ll_goods = (LinearLayout) rootView.findViewById(R.id.ll_goods);
            this.iv_add = (ImageView) rootView.findViewById(R.id.iv_add);
            this.rl_root = (RelativeLayout) rootView.findViewById(R.id.rl_root);
        }

    }

}
